package scorer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.HashMap;

import javax.swing.JPanel;
import scorer.Set.TYPE;
import tile.Tile;

class SetPanel extends JPanel {

	
	public static final int hgap =15,width=50,xoffset=20,yoffset=30;
	public static final Rectangle exit = new Rectangle(275,10,10,10);
	private static final long serialVersionUID = 1L;
	public Set set;
	public static HashMap<String,BufferedImage> images;

	public SetPanel(boolean open,TYPE type) {
		set = new Set(open, type);
		setPreferredSize(new Dimension(200,100));
	}

	public String getSet() {
		return set.getSet();
	}
	
	public boolean finishedSet() {
		return set.isValid();
	}
	
	public boolean isOther() {
		return set.isOther();
	}
	
	public boolean addToSet(Tile t) {
		boolean added = set.addTile(t);
		if(added)
			this.repaint();
		return added;
	}
	
	public Tile getTile(int index) {
		return set.getTile(index);
	}
	
	public Tile removeFromSet(int index) {
		return set.removeTile(index);
	}

	public void paintComponent(Graphics graphics) {
		super.paintComponent(graphics);
		Graphics2D g = (Graphics2D)graphics;
		
		setBackground(Color.black);
		
		g.setColor(Color.white);
		
		g.drawString("Set: "+set.toString(), 2, 15);
		
		g.drawImage(images.get("close_button"), exit.x, exit.y, exit.width, exit.width, this);
		
		int count = 0;
		for (String s : set.getTiles()) {
			if(s.startsWith("w ")) {
				s = s.substring(2);
				g.setColor(Color.cyan);
				for (int i = 0; i < 3; i++) {
					g.drawRect(count * 65 + xoffset - i, yoffset - i,
							width + i, width + i);
				}
			}
			g.drawImage(images.get(s), count*65+xoffset, yoffset, width, width, this);
			count++;
		}
		
	}
	
}